package cn.jsprun.dao.posts;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.jsprun.domain.Threadtypes;
import cn.jsprun.utils.HibernateUtil;

public class ThreadTypesDaoImpl implements ThreadTypesDao {
	public List<Threadtypes> getAllThreadtypes() {
		List<Threadtypes> allThreadtypes = null;
		Transaction tr = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			tr = session.beginTransaction();
			Query query = session.createQuery("from Threadtypes t");
			allThreadtypes = query.list();
			tr.commit();
		} catch (HibernateException he) {
			if(tr!=null){
				tr.rollback();
			}
			he.printStackTrace();
		}
		return allThreadtypes;
	}
	public Threadtypes getThreadtypesbyId(short typeid) {
		Transaction tr = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			tr = session.beginTransaction();
			Threadtypes threadtype = (Threadtypes)session.get(Threadtypes.class, typeid);
			tr.commit();
			return threadtype;
		} catch (HibernateException he) {
			if(tr!=null){
				tr.rollback();
			}
			he.printStackTrace();
		}
		return null;
	}
	public List<Threadtypes> findThreadtypeByHql(String hql) {
		Transaction tr = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			tr = session.beginTransaction();
			Query query = session.createQuery(hql);
			List<Threadtypes> list = query.list();
			tr.commit();
			return list;
		}catch(HibernateException he){
			if(tr!=null){
				tr.rollback();
			}
			he.printStackTrace();
		}
		return null;
	}
}
